package com.senseuni.da.ui.views

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.widget.SwitchCompat
import com.senseuni.da.R

class SettingNotificationItemView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0,
    defStyleRes: Int = 0
) : FrameLayout(context, attrs, defStyleAttr, defStyleRes) {

    private val iconImageView: ImageView
    private val titleLabel: TextView
    private val switchCompat: SwitchCompat

    init {
        inflate(context, R.layout.layout_setting_notification_item_view, this)

        iconImageView = findViewById(R.id.iv_icon)
        titleLabel = findViewById(R.id.tv_title)
        switchCompat = findViewById(R.id.switch_compat)

        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.SettingNotificationItemView, defStyleAttr, defStyleRes)
        val iconResId = typedArray.getResourceId(R.styleable.SettingNotificationItemView_snivIcon, 0)
        val titleText = typedArray.getString(R.styleable.SettingNotificationItemView_snivTitle) ?: ""
        val checked = typedArray.getBoolean(R.styleable.SettingNotificationItemView_snivChecked, true)

        if (iconResId == 0) {
            iconImageView.visibility = View.GONE
        } else {
            iconImageView.setImageResource(iconResId)
        }
        titleLabel.text = titleText

        switchCompat.isChecked = checked

        typedArray.recycle()
    }

    fun setIconResource(resId: Int) {
        iconImageView.setImageResource(resId)
    }

    fun setTitle(text: String) {
        titleLabel.text = text
    }

    fun setTitle(resId: Int) {
        titleLabel.text = context.getString(resId)
    }

    fun setTitleColor(color: Int) {
        titleLabel.setTextColor(color)
    }

    fun setChecked(checked: Boolean) {
        switchCompat.isChecked = checked
    }

    fun isChecked(): Boolean {
        return switchCompat.isChecked
    }
}
